package com.ai.ly.goodsautocat.vo;

import com.alibaba.fastjson.JSONObject;

public class Fscore {
    
    /**实际是true，预测也是true 的数量*/
    public int truePositive;
    /**实际是false，预测是true 的数量*/
    public int falsePositive;
    
    /**实际是true，预测是false 的数量*/
    public int falseNegative;
    /**实际是false，预测也是false 的数量*/
    public int trueNegative;
    
    public Fscore(int truePositive, int falsePositive, int falseNegative, int trueNegative) {
        super();
        this.truePositive = truePositive;
        this.falsePositive = falsePositive;
        this.falseNegative = falseNegative;
        this.trueNegative = trueNegative;
    }
    
    /**
     * 计算准确率
     * @return 准确率=truePositive/(truePositive+falsePositive)
     * @author liangyi
     */
    public double getPrecision() {
        double result=((double)truePositive)/(truePositive+falsePositive);
        return result;
    }
    
    /**
     * 计算召回率
     * @return 召回率=truePositive/(truePositive+falseNegative)
     * @author liangyi
     */
    public double getRecall() {
        double result=((double)truePositive)/(truePositive+falseNegative);
        return result;
    }
    
    /**
     * 计算F1值（F score）
     * @return F1=2PR/(P+R)
     * @author liangyi
     */
    public double getF1() {
        double p=getPrecision();
        double r=getRecall();
        
        double f=2*p*r/(p+r);
        return f;
    }
    
    public double getSampleAccuracy() {
        double result=((double)truePositive+(double)trueNegative)/(truePositive+falsePositive+falseNegative+trueNegative);
        return result;
    }

    public String toString() {
        String json=JSONObject.toJSONString(this);
        return json;
    }
    
    public static void main(String[] args) {
        Fscore f=new Fscore(100, 100, 50, 50);
        System.out.println(f);
    }
    
}
